package com.xiaohe.gulimall.product.service.impl;

import com.xiaohe.gulimall.product.entity.AttrEntity;
import com.xiaohe.gulimall.product.service.AttrService;
import com.xiaohe.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.xiaohe.gulimall.product.vo.SkuItemVo;
import com.xiaohe.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohe.common.utils.PageUtils;
import com.xiaohe.common.utils.Query;

import com.xiaohe.gulimall.product.dao.AttrGroupDao;
import com.xiaohe.gulimall.product.entity.AttrGroupEntity;
import com.xiaohe.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    //获取分类属性分组
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(catelogId ==null){
            IPage<AttrGroupEntity> page = new Query<AttrGroupEntity>().getPage(params);
            baseMapper.selectPage(page,wrapper);
            return new PageUtils(page);
        }else {
            //检索条件
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper = wrapper.eq("catelog_id", catelogId);
            if(!StringUtils.isEmpty(key)){
                queryWrapper.and((obj)->{
                     obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                 });
             }
            IPage<AttrGroupEntity> page = new Query<AttrGroupEntity>().getPage(params);
            baseMapper.selectPage(page,queryWrapper);
            return new PageUtils(page);
        }
    }

    //根据三级分类查询所有的分组和组所对应的属性
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCateLogId(Long catelogId) {
        //查询分组信息
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catelogId);
        //查询所有分组
        List<AttrGroupEntity> list = this.list(queryWrapper);

        List<AttrGroupWithAttrsVo> collect = list.stream().map(item -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item,attrGroupWithAttrsVo);
            //按照分组的id
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查出当前Spu对应的所有分组信息，以及分组对应的所有属性的值
     * @param spuId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、当前spu有多少对应的属性分组
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;
    }

}